//
//  QDFeedbackController.m
//  simulateQuickDoctor
//
//  Created by 姚华杰 on 2017/3/24.
//  Copyright © 2017年 PipiXia. All rights reserved.
//

#import "QDFeedbackController.h"
#import "QDFeedbackCell.h"
#import <SVProgressHUD.h>

@interface QDFeedbackController () <UITextViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) UITextView *textView;

@property (strong, nonatomic) UILabel *lb;

@property (weak, nonatomic) IBOutlet UITextField *textLabel;


@property(nonatomic,strong) NSMutableArray *modelList;
@end

@implementation QDFeedbackController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [_tableView registerNib:[UINib nibWithNibName:@"QDFeedbackCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (void)setupUI{
    
//    self.navigationItem.title = @"意见反馈";
    
    UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,140)];
    
    _textView = tv;
    
    tv.delegate = self;
    
    tv.backgroundColor = [UIColor whiteColor];
    
    _lb = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 30)];
    
    _lb.text = @"请输入你宝贵的意见";
    
    _lb.font = [UIFont systemFontOfSize:14];
    
    _lb.enabled = NO;
    
    _lb.backgroundColor = [UIColor whiteColor];
    
    [tv addSubview:self.lb];
    
    [self.view addSubview:tv];
    _modelList = [NSMutableArray array];
    
    _tableView.dataSource = self;
    
    _tableView.separatorStyle = NO;
    
    //创建右侧提交按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(click)];


    
}

- (void)click{
    
    
    //隐藏键盘
    [self.textView resignFirstResponder];
    
    //文本长度
    NSInteger length = _textView.text.length;
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    
    //没有输入文字时
    if (  length == 0 || [_textView.text isEqualToString:@"请输入你的宝贵意见.."]) {
        
        UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:@"请输入" preferredStyle:UIAlertControllerStyleAlert];
        
        [ac addAction:action];
        
        [self presentViewController:ac animated:YES completion:nil];
        
    //    没有超出文字时,正常上传
    }else if (length <= 30 && length != 0){
        
//        UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:@"正在进行提交.." preferredStyle:UIAlertControllerStyleAlert];
//        
//        [ac addAction:action];
//        
//        [self presentViewController:ac animated:YES completion:nil];
        
        [SVProgressHUD showWithStatus:@"正在提交"];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [SVProgressHUD dismiss];
            
            [self.navigationController popViewControllerAnimated:YES];
        });
        //超出文字时
    }else{
        
        UIAlertController *ac = [UIAlertController alertControllerWithTitle:nil message:@"呵呵" preferredStyle:UIAlertControllerStyleAlert];
        
        
        [ac addAction:action];
        

        [self presentViewController:ac animated:YES completion:nil];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QDFeedbackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    if(indexPath.row == 0)
    {
        cell.imgView.image = [UIImage imageNamed:@"newHospital"];
        
        cell.pointLabel.text = @"联系我们";
    }
    else
    {
        //@"patient_defaultphoto_female"
        
        cell.imgView.image = [UIImage imageNamed:@"patient_defaultphoto_female"];
        
        cell.pointLabel.text = @"用户交流群";
    }
    
 
    
    return cell;
}

#pragma mark - UITextView代理方法
//开始编辑时调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        //按回车取消第一相应者
    }
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    
    self.lb.alpha = 0;//开始编辑时

    return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    //将要停止编辑(不是第一响应者时)
    if (textView.text.length == 0) {
        
        self.lb.alpha = 1;
    }
    return YES;
}

//发生改变时调用
-(void)textViewDidChange:(UITextView *)textView{
    
    NSInteger length = 30 - _textView.text.length;
    
    _textLabel.text = [NSString stringWithFormat:@"还能输入%zd个字",length];
    
     _textLabel.textColor = length >= 0 ? [UIColor grayColor] :[UIColor redColor];
    
     _textLabel.text = length >= 0 ? [NSString stringWithFormat:@"还能输入%zd个字",length] : [NSString stringWithFormat:@"已经超出%zd个字",length];
}

@end
